module server.Timer;

import winapi.winapi;

void run_once_timer(long msecs, void delegate(void *data) dg, void *arg = null)
{
	HANDLE timer = CreateWaitableTimerW(null, FALSE, null);
	long firetime = msecs * 10;
	
	struct timer_args
	{
		void delegate(void *data) dg;
		void *arg;
	}

	extern (Windows)
	static void timer_fired(
	        LPVOID args,
	        DWORD dwTimerLowValue,
	        DWORD dwTimerHighValue
	)
	{
		timer_args *ta = cast(timer_args*)args;
		ta.dg(ta.arg);
		delete ta;
	}
	
	timer_args *args = new timer_args;
	args.dg = dg;
	args.arg = arg;
	
	SetWaitableTimer(timer, &firetime, 0, &timer_fired, cast(void *)(args), FALSE);
}